
<template>
  <el-row type="flex" justify="space-between" style="width:100%">
    <el-col>
      <div class="grid-content bg-purple-dark">
        <span v-if="isRoot"><i class="el-icon-box" />
          <strong>{{ treeNode.name }}</strong>
        </span>
        <span v-else>{{ treeNode.name }}</span>
      </div>
    </el-col>
    <!-- 两个内容 -->
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col><span>{{ treeNode.manager }}</span></el-col>
        <el-col>
          <el-dropdown @command="operateDepts">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">
                <el-button type="text">添加子部门</el-button>
              </el-dropdown-item>
              <el-dropdown-item command="examine">
                <el-button v-if="!isRoot" type="text">查看部门</el-button>
              </el-dropdown-item>
              <el-dropdown-item command="del">
                <el-button v-if="!isRoot" type="text">删除部门</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>

</template>

<script>
import { delDepartment } from '@/api/department'
export default {
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  created() {
  },
  methods: {
    operateDepts(type) {
      if (type === 'add') {
        // 新增
        this.$emit('showdialog', this.treeNode)
      } else if (type === 'examine') {
        // 编辑
        this.$emit('showdialog', this.treeNode)
      } else {
        // 删除
        // console.log(333)

        this.$confirm('是否要删除该部门', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delDepartment(this.treeNode.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.$emit('delDepartment')
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }

  }
}
</script>

<style>

</style>
